<?php

//----------------------------------
// Some general checks to make sure 
// we're in an expected environment:
//----------------------------------
if (get_magic_quotes_gpc()) {
	echo "<p>Error: magic_quotes_gpc is set to 'On' in php.ini.  This site needs it set to 'Off'.";
	exit();
}

if (!(bool) ini_get('short_open_tag')) {
	echo "<p>Error: short_open_tag is set to 'Off' in php.ini.  This site needs it set to 'On'.";
	exit();
}

date_default_timezone_set("America/Chicago");




//-----------------------------------
// Common vars, here only because 
// this file is included everywhere.
//-----------------------------------

$site_const = array();

// Actual definitions are kept outside of this file,
// so they can be kept out of source control.
// To go the extra mile,
require_once('inc_siteconst.php');

// Function for reading any site const, from any page.
function SiteConst($string) {
	global $site_const;

	if (is_null($site_const[$string])) {
		echo "<p>Error: site const value is undefined.  '" . $string . "'";
		exit();
	}
	else {
		return $site_const[$string];
	}
}


// Increment when any css changes.  To reload browser caches.
$CSS_VERSION = "1.3";






//----------------------------------
// Database class.
//----------------------------------
class DatabaseObject {

	public $Conn;

	private $db_server = "";
	private $db_user = "";
	private $db_pass = "";
	private $db_dbname = "";
	
	function __construct() {
		$this->db_server = SiteConst('db_server');
		$this->db_user = SiteConst('db_user');
		$this->db_pass = SiteConst('db_pass');
		$this->db_dbname = SiteConst('db_dbname');
	}

	public function OpenConnection() {
		$this->Conn = new mysqli($this->db_server, $this->db_user, $this->db_pass, $this->db_dbname);

		// Check for connectivity Errors
		if (mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
		}
	}

	public function CloseConnection() {
		// Disconnect from DB.
		$this->Conn->close();
	}

	public function SendQuery($query_string) {
		mysqli_query($this->Conn, $query_string) or die ("Error: " . $this->Conn->error);
	}

	public function GetResultsAsNestedArray($sSQL, $max_rows) {	
		
		if (empty($max_rows)) {
			echo "<p>Error: max_rows not specified.";
			exit();
		}

		$result = $this->Conn->query($sSQL);

		// Display error and halt if error.
		if (!$result) {
			echo "<p>SQL Error: <br>" . $this->Conn->error;
			exit();
		}

		$row_array = array();
		
		$row_index = 0;
		while ($row_index < $max_rows && $row = $result->fetch_array(MYSQLI_BOTH)) {
			$row_index++;
			array_push($row_array, $row);
		}
		
		$result->close();

		return $row_array;
	}

	public function GetNestedArrayExt($sSQL, $max_rows, $move_rows, &$row_count) {	
		
		if (empty($max_rows)) {
			echo "<p>Error: max_rows not specified.";
			exit();
		}

		$result = $this->Conn->query($sSQL);

		// Display error and halt if error.
		if (!$result) {
			echo "<p>SQL Error: <br>" . $this->Conn->error;
			exit();
		}

		// Skip forward n rows.
		$result->data_seek($move_rows);
		//for ($i=0; $i<$move_rows; $i++) {
			//$this->Conn->next_result();
			//$result->next_result();
		//}

		$row_array = array();
		$row_index = 0;
		while ($row_index < $max_rows && $row = $result->fetch_array(MYSQLI_BOTH)) {
			$row_index++;
			array_push($row_array, $row);
		}

		// Get number of rows.
		if ($row_count !== false) {
			$row_count = $result->num_rows;
		}
		
		$result->close();

		return $row_array;
	}

	public function GetFirstCell($sSQL) {
		$result = $this->Conn->query($sSQL) or die ("Error in GetFirstCell(): '" . $this->Conn->error . "' \r\nComplete SQL: '" . $sSQL . "'");
		$row = $result->fetch_array(MYSQLI_NUM);
		$result->close();
		return $row[0];
	}


}



//----------------------------------
// Other Database-related functions.
//----------------------------------

function ValToSQL_str($input) {

	// Note: magic_quotes_gpc currently is "on" in php.ini.
	// This should really be set to off.
	// (now is off and all is well?  2006-10-02 -BL)
	
	// This function ASSUMES $input is UNESCAPED.

	//return "'" . mysql_real_escape_string($input) . "'";
	return "'" . addslashes($input) . "'";
		// This apparently isn't all-encompasing, and more characters
		// should be escaped than addslashes does.  Unfortunately, 
		// mysql_real_escape_string needs the connection object as a
		// param, and we don't have it in this context.  Dumb that we
		// would have to.

}

function ValToSQL_num($input) {
	if (is_numeric($input) && $input != "") {
		return $input;
	} elseif (is_null($input)) {
		return "NULL";
	} else {
		echo "Error converting value to number ('$input').";
		exit();
	}
}

function ValToSQL_bit($input) {
	if ($input === true || $input === 1) {
		return "1";
	} elseif ($input === false || $input === 0) {
		return "0";
	} else {
		echo "Error converting value to bit.";
		exit();
	}
}



?>